package com.jiamao.goplay.ui.discover;

import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.FontRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.adapter.SearchAdapter;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.DiscoverSearchLayoutBinding;
import com.jiamao.goplay.ui.discover.vm.SearchVM;
import com.jiamao.goplay.util.KeyInputUtils;
import com.netease.nim.uikit.business.team.ui.TeamInfoGridView;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

/*
 搜索
 */
public class SearchFragment extends BaseMVVMFragment<DiscoverSearchLayoutBinding, SearchVM> {


    private  SearchAdapter adapter;
    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public SearchVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(SearchVM.class);
    }
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.discover_search_layout;
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.showProg.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                //index = aBoolean;
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
            //    showPhoto();
                binding.refreshLayout.finishLoadMore();
                binding.refreshLayout.finishRefresh();
            }
        });

    }

    @Override
    public void initData() {
        super.initData();
        adapter = new SearchAdapter(getContext());
        binding.setAdapter(adapter);
        ImmersionBar.with(this). keyboardEnable(true).statusBarDarkFont(true).statusBarView(binding.getRoot().findViewById(R.id.status_bar_view)).init();
        final RefreshLayout refreshLayout = binding.refreshLayout;
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.searchAddr(binding.deLoginPhone.getText().toString(),0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.searchAddr(binding.deLoginPhone.getText().toString(),1);
            }
        });
        binding.deLoginPhone.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //回车键
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    viewModel.searchAddr(binding.deLoginPhone.getText().toString(),0);
                }
                return true;
            }
        });
        binding.deLoginPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(binding.deLoginPhone.getText().toString())){
                    viewModel.observableList.clear();
                    binding.refreshLayout.setEnableRefresh(false);
                    binding.refreshLayout.setEnableLoadMore(false);
                    return;
                }
                adapter.setKey(binding.deLoginPhone.getText().toString());
                binding.refreshLayout.setEnableRefresh(true);
                binding.refreshLayout.setEnableLoadMore(true);
                viewModel.searchAddr(binding.deLoginPhone.getText().toString(),0);

            }
        });

        new Handler().postDelayed(new Runnable(){
            public void run() {
                binding.deLoginPhone.requestFocus();
                KeyInputUtils.showInputMethodWindow(binding.deLoginPhone);
            }
        }, 500);

    }

}
